#region

using System.Data.Entity;

#endregion

namespace VinhSon.Intranet.Bussiness
{
    public class VinhSonContext : DbContext
    {
        public VinhSonContext()
                : base("VinhSondb")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Table: Permission
            modelBuilder.Entity<Permission>()
                        .ToTable("Permission",
                                 "dbo");
            modelBuilder.Entity<Permission>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: Role
            modelBuilder.Entity<Role>()
                        .ToTable("Role",
                                 "dbo");
            modelBuilder.Entity<Role>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: User
            modelBuilder.Entity<User>()
                        .ToTable("User",
                                 "dbo");
            modelBuilder.Entity<User>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Table: UserGroup
            modelBuilder.Entity<UserGroup>()
                        .ToTable("UserGroup",
                                 "dbo");
            modelBuilder.Entity<UserGroup>()
                        .HasKey(c => new
                                         {
                                                 c.Id
                                         });
            // Many to many template
            modelBuilder.Entity<Role>()
                        .HasMany(c => c.Users)
                        .WithMany(c => c.Roles)
                        .Map(c =>
                                 {
                                     c.MapLeftKey("RoleId");
                                     c.MapRightKey("UserId");
                                     c.ToTable("UserRole");
                                 });
            modelBuilder.Entity<Role>()
                        .HasMany(c => c.Permissions)
                        .WithMany(c => c.Roles)
                        .Map(c =>
                                 {
                                     c.MapLeftKey("RoleId");
                                     c.MapRightKey("PermissionId");
                                     c.ToTable("RolePermission");
                                 });
            modelBuilder.Entity<UserGroup>()
                        .HasMany(c => c.Permissions)
                        .WithMany(c => c.UserGroups)
                        .Map(c =>
                                 {
                                     c.MapLeftKey("UsergroupId");
                                     c.MapRightKey("PermissionId");
                                     c.ToTable("UserGroupPermission");
                                 });
            modelBuilder.Entity<UserGroup>()
                        .HasMany(c => c.Users)
                        .WithMany(c => c.UserGroups)
                        .Map(c =>
                                 {
                                     c.MapLeftKey("UsergroupId");
                                     c.MapRightKey("UserId");
                                     c.ToTable("UserUserGroup");
                                 });
        }
    }
}
